/*
 *  template.mm
 *  template
 *
 *  Created by SIO2 Interactive on 8/22/08.
 *  Copyright 2008 SIO2 Interactive. All rights reserved.
 *
 */

#include "template.h"

#include "../src/sio2/sio2.h"


void templateRender( void )
{
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );

	// Your rendering code here...
	unsigned int i = 0;
	
	// WARNING: Concerning the exporter please take 
	// note that ONLY the SELECTED objects that are
	// visible in the CURRENT layer will be exported.
	// Any other objects located in other layers will
	// be ignored. To export:
	//
	// 1. Select a layer within Blender
	// 2. Push 'a' twice to select everything
	// 3. Push 'm' + '1' to send the objects to layer #1
	// 4. Make your selection to export.
	// 5. Click 'Export'
	//
	// Initalize the dictionary used by the
	// parser in order to dispatch the root tags etc...
	// to the appropriate load functions.
	sio2ResourceCreateDictionary( sio2->_SIO2resource );
	
	// Open the archive file bundled withing
	// the .app, pass 1 to the function in order
	// to specify to open this archive that is
	// relative to the executable. In order to 
	// integrate file and distribute them with
	// your application simply drag & drop them
	// into the resource directory.
	sio2ResourceOpen( sio2->_SIO2resource,
					 "aap.sio2", 1 );
	
	// Loop into the archive extracting all the 
	// resources compressed within the fileformat.
	while( i != sio2->_SIO2resource->gi.number_entry )
	{
		// Extract the current file selected within
		// the archive package and put the pointer to 
		// the next one.
		sio2ResourceExtract( sio2->_SIO2resource, NULL );
		++i;
	}
	
	// We are done with the file so close the stream.
	sio2ResourceClose( sio2->_SIO2resource );
	
	// Precalculate all the world position matrix
	// for every object.
	sio2ResourceBindAllMatrix( sio2->_SIO2resource );
	
	// Generate the geometry VBO.
	sio2ResourceGenId( sio2->_SIO2resource );
	
	// Reset all SIO2 resource state pointer.
	sio2ResetState();
	
	// Use the helper function sio2ResourceGet in order to get
	// the pointer to a specific resource within a specific
	// SIO2resource.
	
	SIO2camera *_SIO2camera = ( SIO2camera * )sio2ResourceGet( sio2->_SIO2resource,
															  SIO2_CAMERA,
															  "camera/Camera" );
	
	// Make sure that we found the camera
	if( _SIO2camera )
	{
		// Adjust the perspective, please take not
		// that this operation should only be done 
		// once everytime you are switching cameras.
		sio2Perspective( _SIO2camera->fov,
						sio2->_SIO2window->scl->x / sio2->_SIO2window->scl->y,
						_SIO2camera->cstart,
						_SIO2camera->cend );
		
		// Enter the 3D landscape mode
		sio2WindowEnterLandscape3D();
		{
			// Render our camera.
			sio2CameraRender( _SIO2camera );
			
			// Render all solid object in our scene, please
			// take note that the following types are available
			// for rendering (if your scene contain them)
			//
			//
			// SIO2_RENDER_SOLID_OBJECT: Render all visible solid objects.
			//
			// SIO2_RENDER_TRANSPARENT_OBJECT: Render all visible transparent objects.
			//
			// SIO2_RENDER_ALPHA_TESTED_OBJECT: Render all visible alpha tested objects.
			//
			// SIO2_RENDER_NO_MATERIAL: Do not use material when render objects.
			//
			// SIO2_RENDER_NO_MATRIX: Do not use precalculated matrix when render objects. So you can use your own transformations.
			//
			// SIO2_RENDER_CLIPPED_OBJECT: Render object outside the frustum. Objects will not be drawed but physic data will be updated.
			//
			// SIO2_RENDER_LAMP: Render all visible lamps.
			//
			// SIO2_RENDER_EMITTER: Render all visible particle systems.
			//
			// SIO2_RENDER_IPO: Render and update all IPO that are playing.
			//
			// SIO2_EVALUATE_SENSOR: Evaluate sensors and dispatch to events callbacks.
			//
			// SIO2_EVALUATE_TIMER: Evaluation and update timer and dispatch events callbacks
			//
			// SIO2_UPDATE_SOUND_STREAM: Update the sound buffer (only for quad buffered sound streams)
			//
			sio2ResourceRender( sio2->_SIO2resource,
							   sio2->_SIO2window,
							   _SIO2camera,
							   SIO2_RENDER_SOLID_OBJECT );
		}
		// Leave the landscape mode.
		sio2WindowLeaveLandscape3D();
	}
	
	
}

 

void templateShutdown( void )
{
	// Clean up
	sio2ResourceUnloadAll( sio2->_SIO2resource );

	sio2->_SIO2resource = sio2ResourceFree( sio2->_SIO2resource );

	sio2->_SIO2window = sio2WindowFree( sio2->_SIO2window );
	
	sio2 = sio2Shutdown();
  
	printf("\nSIO2: shutdown...\n" );
}


void templateScreenTap( void *_ptr, unsigned char _state )
{

}


void templateScreenTouchMove( void *_ptr )
{


}


void templateScreenAccelerometer( void *_ptr )
{


}
